﻿using FlexFlow.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlexFlow.Runtime
{
    /// <summary>
    /// 表示工作流任务处理器
    /// </summary>
    internal class WorkFlowTaskManager : ManagerBase
    {
        private NotificationCatalogFactory catInfoFactory = new NotificationCatalogFactory()
        {
            Sender = "任务管理器",
            DefaultDescription = "任务处理通知",
            DefaultType = NotificationType.Info
        };
        /// <summary>
        /// 获取或设置默认的工作流任务处理器
        /// </summary>
        public static WorkFlowTaskManager Default { get; set; }

        /// <summary>
        /// 发送指定的任务
        /// </summary>
        /// <param name="tsk">
        /// 要发送的任务
        /// </param>
        /// <returns>
        /// 操作结果，其中tag为未成功发送通知的用户Id集合
        /// </returns>
        internal OperateResult SendTask(WorkFlowTask tsk)
        {
            //发送任务
            bool isTaskSaveSuccess = this.Components.WorkFlowTaskProvider.Add(tsk);
            if (!isTaskSaveSuccess)
            {
                return new OperateResult() { ResultCode = 0x110101, Message = "保存任务失败！" };
            }
            //通知发送失败的用户集合
            List<Guid> notiFailedUserList = new List<Guid>();
            try
            {
                //向用户发送通知
                if (tsk.Reciver != string.Empty)
                {
                    var recivers = tsk.Reciver.Split(',')
                        .Where(m => m != "")
                        .Select(m => Guid.Parse(m));
                    foreach (var reciver in recivers)
                    {
                        NotificationCatalog catalog = this.catInfoFactory.CreateCatalog(reciver);
                        OperateResult sendRes = NotificationManager.Default.SendNotification(catalog, "有任务到达", "你有新的任务到达，请尽快处理！", tsk.Identity);
                        if (sendRes.ResultCode != 200)
                        {
                            notiFailedUserList.Add(reciver);
                        }
                    }
                }
                return OperateResult.SuccessResult(notiFailedUserList);
            }
            catch (Exception ex)
            {
                return new OperateResult()
                {
                    ResultCode = 0x110102,
                    Message = "任务发送出错：" + ex.Message
                };
            }
        }
        /// <summary>
        /// 取得指定用户发送的任务列表
        /// </summary>
        /// <param name="sender">发送者</param>
        /// <returns>
        /// 操作结果
        /// </returns>
        internal OperateResult GetSendTaskList(Guid sender)
        {
            return OperateResult.SuccessResult(this.Components.WorkFlowTaskProvider.GetTaskBySender(sender));
        }
        /// <summary>
        /// 取得发送给指定用户的任务列表
        /// </summary>
        /// <param name="reciver">
        /// 接收者
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        internal OperateResult GetReciveTaskList(Guid reciver)
        {
            return OperateResult.SuccessResult(this.Components.WorkFlowTaskProvider.GetTaskByReciver(reciver));
        }


        static WorkFlowTaskManager()
        {
            Default = new WorkFlowTaskManager();
        }
    }
}
